#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 1e4 + 10;

int n, m, k;
int eva[N], num[N];
int dp[220][N];

// dp:最长公共子序列
// dp[i][j]:eva中前i个数字和num中前j个数字相匹配的最大长度
inline void solution() {
    mm(dp, 0);
    cin >> n >> m;
    for (int i = 1; i <= m; i ++ ) cin >> eva[i];
    cin >> k;
    for (int i = 1; i <= k; i ++ ) cin >> num[i];

    // 状态转移:
    // 如果相同的话:dp[i][j] = max(dp[i - 1][j], dp[i][j - 1] + 1);
    for (int i = 1; i <= m; i ++ ) {
        for (int j = 1; j <= k; j ++ ) {
            if (eva[i] == num[j]) dp[i][j] = max(dp[i - 1][j], dp[i][j - 1] + 1);
            else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
    }
   
    cout << dp[m][k];
}

int main() {
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}